package com.daxia.ogmall.web.controller.m;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.ogmall.common.MyJSONObject;
import com.daxia.ogmall.dto.CompanyDTO;
import com.daxia.ogmall.dto.DesignDTO;
import com.daxia.ogmall.model.Company;
import com.daxia.ogmall.support.MPage;
import com.daxia.ogmall.support.Page;

@Controller
@RequestMapping(value = "/m/company", produces = "application/json;charset=UTF-8")
public class MCompanyController extends MBaseController {
    
    @ResponseBody
    @RequestMapping("list")
    public String list(CompanyDTO dto, MPage page) throws Exception {
    	MyJSONObject data = new MyJSONObject();
    	JSONArray companyInfos = new JSONArray();
    
        List<CompanyDTO> companyDTOs = companyService.find(dto, page);
        for (CompanyDTO companyDTO : companyDTOs) {
        	JSONObject companyInfo = new JSONObject();
        	companyInfo.put("id", companyDTO.getId());
        	companyInfo.put("headImage", companyDTO.getHeadImage());
        	companyInfo.put("name", companyDTO.getName());
        	companyInfo.put("latitude", companyDTO.getLatitude());
        	companyInfo.put("longitude", companyDTO.getLongitude());
        	String image = "";
        	String images = companyDTO.getImages();
        	if (images == null) {
        		images = "";
        	}
        	String[] imgs = images.split(",");
        	if (imgs.length > 0) {
        		image = imgs[0];
        	}
        	companyInfo.put("image", image);
        	companyInfo.put("dealCount", designService.countByCompanyId(companyDTO.getId()));
        	
        	
        	companyInfos.add(companyInfo);
        }
        
        data.put("companyInfos", companyInfos);
        return toJson(data);
    }
    
    @ResponseBody
    @RequestMapping("detail")
    public String detail(Long id) throws Exception {
    	
    	CompanyDTO companyDTO = companyService.load(id);
    	MyJSONObject obj = new MyJSONObject();
    	obj.put("name", companyDTO.getName());
    	obj.put("headImage", companyDTO.getHeadImage());
    	String images = companyDTO.getImages();
    	String[] imgs = {};
    	if (StringUtils.isNotBlank(images)) {
    		imgs = images.split(",");
    	}
    	obj.put("images", imgs);
    	obj.put("mobile", companyDTO.getMobile());
    	obj.put("latitude", companyDTO.getLatitude());
    	obj.put("longitude", companyDTO.getLongitude());
    	obj.put("details", companyDTO.getDetails());
    	String address = getAddress(companyDTO);
		obj.put("address", address);
    	
    	MyJSONObject province = new MyJSONObject();
    	if (companyDTO.getProvince() != null) {
    		province.put("id", companyDTO.getProvince().getId());
    		province.put("name", companyDTO.getProvince().getName());
    	}
    	obj.put("provinceInfo", province);
    	
    	MyJSONObject city = new MyJSONObject();
    	if (companyDTO.getCity() != null) {
    		city.put("id", companyDTO.getCity().getId());
    		city.put("name", companyDTO.getCity().getName());
    	}
    	obj.put("cityInfo", city);
    	
    	MyJSONObject district = new MyJSONObject();
    	if (companyDTO.getDistrict() != null) {
    		district.put("id", companyDTO.getDistrict().getId());
    		district.put("name", companyDTO.getDistrict().getName());
    	}
    	obj.put("districtInfo", district);
    	
    	DesignDTO designQuery = new DesignDTO();
		Page designPage = new Page();
		Company company = new Company();
		BeanUtils.copyProperties(companyDTO, company);
		designQuery.setCompany(company );
		List<DesignDTO> designList = designService.find(designQuery , designPage);
		JSONArray designInfos = new JSONArray();
		for (DesignDTO designDTO : designList) {
			JSONObject designInfo = new JSONObject();
			designInfo.put("id", designDTO.getId());
			designInfo.put("headImage", designDTO.getHeadImage());
			designInfos.add(designInfo);
		}
		obj.put("designInfos", designInfos);
    	return toJson(obj);
    }

	private String getAddress(CompanyDTO companyDTO) {
		String address = "";
		
		if (companyDTO.getProvince() != null) {
    		address += companyDTO.getProvince().getName();
    	}
    	if (companyDTO.getCity() != null) {
    		address += companyDTO.getCity().getName();
    	}
    	if (companyDTO.getDistrict() != null) {
    		address += companyDTO.getDistrict().getName();
    	}
    	if (StringUtils.isNotBlank(companyDTO.getRegisterAddress())) {
    		address += companyDTO.getRegisterAddress();
    	}
		return address;
	}
    
}
